package it.geosolutions.geoserver.rest.vgis;
import it.geosolutions.geoserver.rest.GeoServerRESTManager;
import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;

import java.io.File;
import java.net.URL;
import java.util.Map;

/**
 * @author chenxw
 * @version 1.0
 * @date 2021/11/8 14:02
 */
public class GeoServerUtil {


    //发布矢量shp服务
    public static String publishShpService(String shpzipPath,String geoserverHost,String geoserverPort,
                                           String geoserverUserName,String geoserverPassWord,
                                           String geoserverWorkspace,String defaultPtStyleName,
                                           String defaultLineStyleName,String defaultPolyStyleName){
        String serviceUrl=null;
        Boolean publishResult=false;
        try
        {
            File shpzipFile=new File(shpzipPath);
            GeoServerRESTManager manager = null;
            URL u = new URL("http://"+geoserverHost+":"+geoserverPort+"/geoserver");
            manager = new GeoServerRESTManager(u, geoserverUserName,geoserverPassWord);
            GeoServerRESTPublisher publisher=manager.getPublisher();
            String newShpZipFilePath=ZipUtils.renameZipFile(shpzipPath);
            File newShpzipFile=new File(newShpZipFilePath);
            String newShpFileName=newShpzipFile.getName();
            String serviceName=FileUtils.getFileNameRemoveSubfix(newShpFileName);
            String newShpFilePath=shpzipFile.getParent() + File.separator + serviceName+File.separator+serviceName+".shp";
            Map<String, Object> map = GdalUtils.getShpSpatailMetaInfo(newShpFilePath);
            //String coordName = (String)map.get("coordName");
            String espgNumber= (String)map.get("espg");
            //如果不能自动读取espg,设置为大地2000的默认坐标系
            if(espgNumber==null)
                espgNumber="4490";
            String geomType=(String)map.get("geometryType");
            String defaultStyle = "";
            if (geomType.toLowerCase().contains("polygon"))
                defaultStyle = defaultPolyStyleName;
            else if (geomType.toLowerCase().contains("line"))
                defaultStyle = defaultLineStyleName;
            else if (geomType.toLowerCase().contains("point"))
                defaultStyle =defaultPtStyleName;
            publishResult = publisher.publishShp(geoserverWorkspace, serviceName, serviceName, newShpzipFile, "EPSG:" + espgNumber, defaultStyle);
            if(publishResult){
                double minx=(double)map.get("minx");
                double maxx=(double)map.get("maxx");
                double miny=(double)map.get("miny");
                double maxy=(double)map.get("maxy");
                String issueUrl = "http://"+geoserverHost+":"+geoserverPort+"/geoserver/" + geoserverWorkspace +"/wms?";
                StringBuilder sb = new StringBuilder(issueUrl);
                sb.append("service=WMS&version=1.1.0&request=GetMap&layers=").append(geoserverWorkspace).append(":").append(serviceName);
                sb.append("&bbox=").append(minx).append(",").append(miny).append(",").append(maxx).append(",").append(maxy);
                sb.append("&width=689&height=768&srs=EPSG:").append(espgNumber);
                sb.append("&styles=&format=application/openlayers");
                serviceUrl = sb.toString();
            }
        }
        catch (Exception ex){

        }
        return serviceUrl;
    }

    //发布tif影像服务
    public static String publishTifService(String tifPath,String geoserverHost,String geoserverPort,
                                            String geoserverUserName,String geoserverPassWord,
                                            String geoserverWorkspace){
        String serviceUrl=null;
        Boolean publishResult=false;
        try
        {
            File file=new File(tifPath);
            GeoServerRESTManager manager = null;            ;
            URL u = new URL("http://"+geoserverHost+":"+geoserverPort+"/geoserver");
            manager = new GeoServerRESTManager(u, geoserverUserName,geoserverPassWord);
            GeoServerRESTPublisher publisher=manager.getPublisher();

            String fileName=file.getName();
            String coverageName=FileUtils.getFileNameRemoveSubfixAddUUid(fileName);
            publishResult = publisher.publishGeoTIFF(geoserverWorkspace, coverageName, coverageName, file);
            if(publishResult){
                Map<String, Object> map = GdalUtils.getImgSpatailMetaInfo(tifPath);
                String coordName = (String)map.get("coordName");
                int xsize = (Integer)map.get("xsize");
                int ysize = (Integer)map.get("ysize");
                double[] transform = (double[])map.get("transform");
                double v0 = transform[0];
                double v1 = transform[1];
                double v2 = transform[2];
                double v4 = transform[4];
                double v3 = transform[3];
                double v5 = transform[5];
                String espgCode="4490";
                if(coordName.equalsIgnoreCase("WGS 84"))
                    espgCode="4326";
                if(coordName.equalsIgnoreCase("Beijing 54"))
                    espgCode="2437";
                if(coordName.equalsIgnoreCase("China Geodetic Coordinate System 2000"))
                    espgCode="4490";
                if(coordName.equalsIgnoreCase("Xian 1980"))
                    espgCode="4360";
                double minx=v0;
                double maxx=v0 + xsize * v1 + ysize * v2;
                double miny=v3 + xsize * v4 + ysize * v5;
                double maxy=v3;
                String issueUrl = "http://"+geoserverHost+":"+geoserverPort+"/geoserver/" + geoserverWorkspace +"/wms?";
                StringBuilder sb = new StringBuilder(issueUrl);
                sb.append("service=WMS&version=1.1.0&request=GetMap&layers=").append(geoserverWorkspace).append(":").append(coverageName);
                sb.append("&bbox=").append(minx).append(",").append(miny).append(",").append(maxx).append(",").append(maxy);
                sb.append("&width=689&height=768&srs=EPSG:").append(espgCode);
                sb.append("&styles=&format=application/openlayers");
                serviceUrl = sb.toString();
            }
        }
        catch (Exception ex){

        }
        return serviceUrl;
    }

    //发布erdas img影像服务
    public static String publishImgService(String imgPath,String geoserverHost,String geoserverPort,
                                           String geoserverUserName,String geoserverPassWord,
                                           String geoserverWorkspace){
        String serviceUrl=null;
        Boolean publishResult=false;
        try
        {
            File file=new File(imgPath);
            GeoServerRESTManager manager = null;
            URL u = new URL("http://"+geoserverHost+":"+geoserverPort+"/geoserver");
            manager = new GeoServerRESTManager(u, geoserverUserName,geoserverPassWord);
            GeoServerRESTPublisher publisher=manager.getPublisher();

            String fileName=file.getName();
            String coverageName=FileUtils.getFileNameRemoveSubfixAddUUid(fileName);
            publishResult = publisher.publishImg(geoserverWorkspace, coverageName, coverageName, file);
            if(publishResult){
                Map<String, Object> map = GdalUtils.getImgSpatailMetaInfo(imgPath);
                String coordName = (String)map.get("coordName");
                int xsize = (Integer)map.get("xsize");
                int ysize = (Integer)map.get("ysize");
                double[] transform = (double[])map.get("transform");
                double v0 = transform[0];
                double v1 = transform[1];
                double v2 = transform[2];
                double v4 = transform[4];
                double v3 = transform[3];
                double v5 = transform[5];
                String espgCode="4490";
                if(coordName.equalsIgnoreCase("WGS 84"))
                    espgCode="4326";
                if(coordName.equalsIgnoreCase("Beijing 54"))
                    espgCode="2437";
                if(coordName.equalsIgnoreCase("China Geodetic Coordinate System 2000"))
                    espgCode="4490";
                if(coordName.equalsIgnoreCase("Xian 1980"))
                    espgCode="4360";
                double minx=v0;
                double maxx=v0 + xsize * v1 + ysize * v2;
                double miny=v3 + xsize * v4 + ysize * v5;
                double maxy=v3;
                String issueUrl = "http://"+geoserverHost+":"+geoserverPort+"/geoserver/" + geoserverWorkspace +"/wms?";
                StringBuilder sb = new StringBuilder(issueUrl);
                sb.append("service=WMS&version=1.1.0&request=GetMap&layers=").append(geoserverWorkspace).append(":").append(coverageName);
                sb.append("&bbox=").append(minx).append(",").append(miny).append(",").append(maxx).append(",").append(maxy);
                sb.append("&width=689&height=768&srs=EPSG:").append(espgCode);
                sb.append("&styles=&format=application/openlayers");
                serviceUrl = sb.toString();
            }
        }
        catch (Exception ex){

        }
        return serviceUrl;
    }

    //发布erdas img影像服务
    public static String publishEnviService(String enviFilePath,String geoserverHost,String geoserverPort,
                                           String geoserverUserName,String geoserverPassWord,
                                           String geoserverWorkspace){
        String serviceUrl=null;
        Boolean publishResult=false;
        try
        {
            File file=new File(enviFilePath);
            GeoServerRESTManager manager = null;
            URL u = new URL("http://"+geoserverHost+":"+geoserverPort+"/geoserver");
            manager = new GeoServerRESTManager(u, geoserverUserName,geoserverPassWord);
            GeoServerRESTPublisher publisher=manager.getPublisher();

            String fileName=file.getName();
            String coverageName=FileUtils.getFileNameRemoveSubfixAddUUid(fileName);
            publishResult = publisher.publishEnvi(geoserverWorkspace, coverageName, coverageName, file);
            if(publishResult){
                Map<String, Object> map = GdalUtils.getImgSpatailMetaInfo(enviFilePath);
                String coordName = (String)map.get("coordName");
                int xsize = (Integer)map.get("xsize");
                int ysize = (Integer)map.get("ysize");
                double[] transform = (double[])map.get("transform");
                double v0 = transform[0];
                double v1 = transform[1];
                double v2 = transform[2];
                double v4 = transform[4];
                double v3 = transform[3];
                double v5 = transform[5];
                String espgCode="4490";
                if(coordName.equalsIgnoreCase("WGS 84"))
                    espgCode="4326";
                if(coordName.equalsIgnoreCase("Beijing 54"))
                    espgCode="2437";
                if(coordName.equalsIgnoreCase("China Geodetic Coordinate System 2000"))
                    espgCode="4490";
                if(coordName.equalsIgnoreCase("Xian 1980"))
                    espgCode="4360";
                double minx=v0;
                double maxx=v0 + xsize * v1 + ysize * v2;
                double miny=v3 + xsize * v4 + ysize * v5;
                double maxy=v3;
                String issueUrl = "http://"+geoserverHost+":"+geoserverPort+"/geoserver/" + geoserverWorkspace +"/wms?";
                StringBuilder sb = new StringBuilder(issueUrl);
                sb.append("service=WMS&version=1.1.0&request=GetMap&layers=").append(geoserverWorkspace).append(":").append(coverageName);
                sb.append("&bbox=").append(minx).append(",").append(miny).append(",").append(maxx).append(",").append(maxy);
                sb.append("&width=689&height=768&srs=EPSG:").append(espgCode);
                sb.append("&styles=&format=application/openlayers");
                serviceUrl = sb.toString();
            }
        }
        catch (Exception ex){

        }
        return serviceUrl;
    }

    //发布 jpg 影像服务
    public static String publishJpgService(String jpgzipPath,String geoserverHost,String geoserverPort,
                                           String geoserverUserName,String geoserverPassWord,
                                           String geoserverWorkspace){
        String serviceUrl=null;
        Boolean publishResult=false;
        try
        {
            File jpgzipFile=new File(jpgzipPath);
            GeoServerRESTManager manager = null;
            URL u = new URL("http://"+geoserverHost+":"+geoserverPort+"/geoserver");
            manager = new GeoServerRESTManager(u, geoserverUserName,geoserverPassWord);
            GeoServerRESTPublisher publisher=manager.getPublisher();
            String newJpgZipFilePath=ZipUtils.renameZipFile(jpgzipPath);
            File newJpgzipFile=new File(newJpgZipFilePath);
            String fileName=newJpgzipFile.getName();
            //String coverageName=FileUtils.getFileNameRemoveSubfixAddUUid(fileName);
            String coverageName=FileUtils.getFileNameRemoveSubfix(fileName);
            publishResult = publisher.publishWorldImage(geoserverWorkspace, coverageName, newJpgzipFile);

            if(publishResult){
                String jpgFilePath=ZipUtils.unZipAndGetFileBySuffix(jpgzipPath,".jpg");
                Map<String, Object> map = GdalUtils.getImgSpatailMetaInfo(jpgFilePath);
                String coordName = (String)map.get("coordName");
                int xsize = (Integer)map.get("xsize");
                int ysize = (Integer)map.get("ysize");
                double[] transform = (double[])map.get("transform");
                double v0 = transform[0];
                double v1 = transform[1];
                double v2 = transform[2];
                double v4 = transform[4];
                double v3 = transform[3];
                double v5 = transform[5];
                String espgCode="4490";
                if(coordName.equalsIgnoreCase("WGS 84"))
                    espgCode="4326";
                if(coordName.equalsIgnoreCase("Beijing 54"))
                    espgCode="2437";
                if(coordName.equalsIgnoreCase("China Geodetic Coordinate System 2000"))
                    espgCode="4490";
                if(coordName.equalsIgnoreCase("Xian 1980"))
                    espgCode="4360";
                double minx=v0;
                double maxx=v0 + xsize * v1 + ysize * v2;
                double miny=v3 + xsize * v4 + ysize * v5;
                double maxy=v3;
                String issueUrl = "http://"+geoserverHost+":"+geoserverPort+"/geoserver/" + geoserverWorkspace +"/wms?";
                StringBuilder sb = new StringBuilder(issueUrl);
                sb.append("service=WMS&version=1.1.0&request=GetMap&layers=").append(geoserverWorkspace).append(":").append(coverageName);
                sb.append("&bbox=").append(minx).append(",").append(miny).append(",").append(maxx).append(",").append(maxy);
                sb.append("&width=689&height=768&srs=EPSG:").append(espgCode);
                sb.append("&styles=&format=application/openlayers");
                serviceUrl = sb.toString();
            }
        }
        catch (Exception ex){

        }
        return serviceUrl;
    }


    //发布 png 影像服务
    public static String publishPngService(String pngzipPath,String geoserverHost,String geoserverPort,
                                           String geoserverUserName,String geoserverPassWord,
                                           String geoserverWorkspace){
        String serviceUrl=null;
        Boolean publishResult=false;
        try
        {
            File pngzipFile=new File(pngzipPath);
            GeoServerRESTManager manager = null;
            URL u = new URL("http://"+geoserverHost+":"+geoserverPort+"/geoserver");
            manager = new GeoServerRESTManager(u, geoserverUserName,geoserverPassWord);
            GeoServerRESTPublisher publisher=manager.getPublisher();
            String newJpgZipFilePath=ZipUtils.renameZipFile(pngzipPath);
            File newJpgzipFile=new File(newJpgZipFilePath);
            String fileName=newJpgzipFile.getName();
            //String coverageName=FileUtils.getFileNameRemoveSubfixAddUUid(fileName);
            String coverageName=FileUtils.getFileNameRemoveSubfix(fileName);
            publishResult = publisher.publishWorldImage(geoserverWorkspace, coverageName, newJpgzipFile);

            if(publishResult){
                String pngFilePath=ZipUtils.unZipAndGetFileBySuffix(pngzipPath,".png");
                Map<String, Object> map = GdalUtils.getImgSpatailMetaInfo(pngFilePath);
                String coordName = (String)map.get("coordName");
                int xsize = (Integer)map.get("xsize");
                int ysize = (Integer)map.get("ysize");
                double[] transform = (double[])map.get("transform");
                double v0 = transform[0];
                double v1 = transform[1];
                double v2 = transform[2];
                double v4 = transform[4];
                double v3 = transform[3];
                double v5 = transform[5];
                String espgCode="4490";
                if(coordName.equalsIgnoreCase("WGS 84"))
                    espgCode="4326";
                if(coordName.equalsIgnoreCase("Beijing 54"))
                    espgCode="2437";
                if(coordName.equalsIgnoreCase("China Geodetic Coordinate System 2000"))
                    espgCode="4490";
                if(coordName.equalsIgnoreCase("Xian 1980"))
                    espgCode="4360";
                double minx=v0;
                double maxx=v0 + xsize * v1 + ysize * v2;
                double miny=v3 + xsize * v4 + ysize * v5;
                double maxy=v3;
                String issueUrl = "http://"+geoserverHost+":"+geoserverPort+"/geoserver/" + geoserverWorkspace +"/wms?";
                StringBuilder sb = new StringBuilder(issueUrl);
                sb.append("service=WMS&version=1.1.0&request=GetMap&layers=").append(geoserverWorkspace).append(":").append(coverageName);
                sb.append("&bbox=").append(minx).append(",").append(miny).append(",").append(maxx).append(",").append(maxy);
                sb.append("&width=689&height=768&srs=EPSG:").append(espgCode);
                sb.append("&styles=&format=application/openlayers");
                serviceUrl = sb.toString();
            }
        }
        catch (Exception ex){

        }
        return serviceUrl;
    }

    
    public static String tileTifService(String tifPath,String geoserverHost,String geoserverPort )
    {
        String serviceUrl=null;
        Boolean publishResult=false;
        try {
            String gwcUrl="http://"+geoserverHost+":"+geoserverPort+"/geoserver/gwc/rest/";


            serviceUrl="http://localhost:8085/geoserver/gwc/demo/nurc:Arc_Sample?gridSet=EPSG:4326&format=image/png";
            serviceUrl="http://localhost:8085/geoserver/gwc/service/wmts?layer=nurc:Arc_Sample&style=&tilematrixset" +
                    "=EPSG:4326&Service=WMTS&Request=GetTile&Version=1.0" +
                    ".0&Format=image/png&TileMatrix=EPSG:4326:21&TileCol=2503412&TileRow=557053";

        }
        catch (Exception ex){

        }
        return serviceUrl;
    }
}
